layui.define(['form', 'table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  var batchId =$('input[name="batchId"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/device/produce/batch/device/list.data'
        ,where:{batchId:batchId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
			{checkbox: true}
            ,{field: 'deviceSn',title: '设备序列号'}
            ,{field: 'deviceFunNames',title: '设备功能项'}
            ,{field: 'userName',title: '设备归属', templet:function(record){
                if(record.userName){
                    return record.userName;
                }else{
                    return '未分配';
                }
             }}
            ,{field: 'belongDate', title: '归属时间'}
            ,{field: 'placeName',title: '设备部署', templet:function(record){
                if(record.placeName){
                    return record.placeName;
                }else{
                    return '---';
                }
              }}
            ,{field: 'deployDate', title: '部署时间'}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 160, align:'center', fixed: 'right', templet:function(record) {
                var html = '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">查看</a>';
                    html = html + '<a class="layui-btn layui-btn-xs" lay-event="qrcode">二维码</a>';
                return html;
            }}
        ]]
        ,page: true
        ,height: 'full'
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        exportAll:function (obj){
            parent.layer.open({
                type: 2
                , title: '导出全部设备信息'
                , content: layui.setter.webRoot + '/device/produce/batch/excel/export.html?batchId='+batchId
                , maxmin: false
                , area: ['500px', '200px']
                , btn: ['确定', '取消']
                , yes: function(index, layero){
                    var iframeWindow =  parent.window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field;
                        $('#exportAllForm input[name="httpDomain"]').val(field.httpDomain);
                        $('#exportAllForm').submit();
                        parent.layer.close(index); //关闭弹层
                    });
                    submit.trigger('click');
                }
            });
        }
		,setBelong:function (obj){
			var checkStatus = table.checkStatus('LAY-table'),checkData = checkStatus.data; //得到选中的数据
			var ids = '';
	        if(checkData.length === 0){
	          	return layer.msg('请选择要设置设备！');
	        }else{
	        	$(checkData).each(function(i,e){ids = ids + ',' + e.deviceSn;});
	        	ids = ids.substring(1);
	        }
			
			$.ajax({
				type: "post",
		        url: layui.setter.webRoot + '/device/produce/batch/device/belong/cache.do',
		        data:{deviceSnListStr:ids},
		        dataType:"json",
		        success:function(res){
	              if(res.code == 200){
					layer.open({
			              type: 2
			              , title: '设置设备所属人'
			              , content: layui.setter.webRoot + '/device/produce/batch/device/belong/set.html'
			              , maxmin: false
			              , area: ['500px', '550px']
			              , btn: ['确定', '取消']
			              , yes: function(index, layero){
			                  var iframeWindow = window['layui-layer-iframe'+ index]
			                      ,submitID = 'LAY-form-submit'
			                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
			                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
			                      var field = data.field;
			                      $.ajax({
			                          type: "post",
			                          url: layui.setter.webRoot + '/device/produce/batch/device/belong/set.do',
			                          data:field,
			                          dataType:"json",
			                          success:function(res){
			                              if(res.code == 200){
			                                  layer.close(index);
			                                  table.reload('LAY-table');
			                                  layer.msg('设置成功',{icon: 1});
			                              }else{
			                                  layer.msg(res.msg,{icon: 2});
			                              }
			                          }
			                      });
			                  });
			                  submit.trigger('click');
			              }
			          });
	              }else{
	                  layer.msg(res.msg,{icon: 2});
	              }
		        }
		    });
	      }
		  ,removeBelong:function (obj){
	  			var checkStatus = table.checkStatus('LAY-table'),checkData = checkStatus.data; //得到选中的数据
	  			var ids = '';
	  	        if(checkData.length === 0){
	  	          	return layer.msg('请选择要移除设备！');
	  	        }else{
	  	        	$(checkData).each(function(i,e){ids = ids + ',' + e.deviceSn;});
	  	        	ids = ids.substring(1);
	  	        }
	  			
	  			$.ajax({
	  				type: "post",
	  		        url: layui.setter.webRoot + '/device/produce/batch/device/belong/cache.do',
	  		        data:{deviceSnListStr:ids},
	  		        dataType:"json",
	  		        success:function(res){
	  	              if(res.code == 200){
	  					layer.open({
	  			              type: 2
	  			              , title: '移除设备所属人'
	  			              , content: layui.setter.webRoot + '/device/produce/batch/device/belong/remove.html'
	  			              , maxmin: false
	  			              , area: ['500px', '400px']
	  			              , btn: ['确定', '取消']
	  			              , yes: function(index, layero){
	  			                  var iframeWindow = window['layui-layer-iframe'+ index]
	  			                      ,submitID = 'LAY-form-submit'
	  			                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
	  			                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
	  			                      var field = data.field;
	  			                      $.ajax({
	  			                          type: "post",
	  			                          url: layui.setter.webRoot + '/device/produce/batch/device/belong/remove.do',
	  			                          data:field,
	  			                          dataType:"json",
	  			                          success:function(res){
	  			                              if(res.code == 200){
	  			                                  layer.close(index);
	  			                                  table.reload('LAY-table');
	  			                                  layer.msg('移除成功',{icon: 1});
	  			                              }else{
	  			                                  layer.msg(res.msg,{icon: 2});
	  			                              }
	  			                          }
	  			                      });
	  			                  });
	  			                  submit.trigger('click');
	  			              }
	  			          });
	  	              }else{
	  	                  layer.msg(res.msg,{icon: 2});
	  	              }
	  		        }
	  		    });
  	      }
        ,info:function(obj){
            parent.layer.open({
                type: 2
                ,title: '查看设备详情'
                ,content: layui.setter.webRoot + '/device/info.html?deviceSn='+obj.data.deviceSn
                ,maxmin: true
                ,area: ['90%', '90%']
            });
        }
        ,qrcode: function (obj) {
            layer.open({
                type: 2
                , title: '设备二维码'
                , content: layui.setter.webRoot + '/device/qrcode.html?deviceSn=' + obj.data.deviceSn
                , maxmin: false
                , area: ['400px', '450px']
                , btn: ['取消']
            });
        }
    }

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });
  
  exports('device_produce_batch_device', {})
});